/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.controller;

import android.car.Car;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.content.Context;

import com.ecarx.systemui.plugin.utils.LogUtils;
import com.ecarx.systemui.plugin.PluginContext;
import com.ecarx.systemui.plugin.utils.ThreadUtils;
import com.ecarx.xui.adaptapi.FunctionStatus;
import com.ecarx.xui.adaptapi.car.IWrapper;


/**
 * IHvacModule.HVAC_FUNC_POWER——开关
 * IHvacModule.HVAC_FUNC_AUTO——自动风量
 * IHvacModule.HVAC_FUNC_AC_MAX——Max AC
 * IHvacModule.HVAC_FUNC_AC——AC
 * IHvacModule.HVAC_FAN_SPEED——手动风量
 * IHvacModule.HVAC_AUTO_FAN_SPEED——自动风量
 * IHvacModule.HVAC_FUNC_BLOWING_TEMP_COLOR——倒车影像(PAC)摄像头类型
 * IHvacModule.HVAC_FUNC_CIRCULATION——循环模式
 * IHvacModule.HVAC_FUNC_DEFROST_FRONT_MAX——最大除霜
 * IHvacModule.HVAC_FUNC_SEAT_VENTILATION——主/副驾座椅通风
 * IHvacModule.HVAC_FUNC_SEAT_HEATING——主/副驾座椅加热
 * IHvacModule.HVAC_FUNC_AUTO_SEAT_VENTILATION——自动座椅通风设置
 * IHvacModule.HVAC_FUNC_TEMP——主/副驾温度值
 * IHvacModule.HVAC_FUNC_BLOWING_MODE——主/副/二排吹风
 * IHvacModule.HVAC_FUNC_ECO_SWITCH——ECO
 * IHvacModule.HVAC_FUNC_STEERING_WHEEL_HEAT——方向盘加热等级
 * IHvacModule.HVAC_FUNC_TEMP_DUAL——温度同步
 * IHvacModule.HVAC_FUNC_AUTO_SEAT_HEATING——自动座椅加热设置
 * IHvacModule.HVAC_FUNC_G_CLEAN——自动清洁？？？
 * IHvacModule.HVAC_FUNC_TEMP_UNIT——温度单位(C/F)
 * IHvacModule.HVAC_FUNC_AQS_SWITCH——车内空气质量控制系统/空气质量传感器开关
 * IAdasModule.WPC_FUNC_WORK_MODE——无线充电
 * IHvacModule.HVAC_FUNC_CLIMATE_ZONE——获取几区空调
 * IAdasModule.WPC_FUNC_CHARGE_STATES——WPC充电状态
 * IPASModule.PAS_FUNC_PAC_CAMERA_TYPE——倒车影像(PAC)摄像头类型
 * ISafetyModule.SETTING_FUNC_CENTRAL_LOCK——中控锁
 * IPASModule.PAS_FUNC_SAP_ACTIVATION——半自动泊车(SAP)开关设置
 * IPASModule.PAS_FUNC_APA_DETECT_PARKING_SPACE——自动泊车是否检测到停车位
 * IHvacModule.HVAC_FUNC_HARDKEY——空调硬按键回调
 */
public class CarSignalController {
    private static final String TAG = CarSignalController.class.getSimpleName();
    private static CarSignalController mInstance;
    private static boolean isInitOk = false;
    private static boolean isInitIng = false;
    private CarPropertyManager carPropertyManager;
    private IWrapper wrapper;

    public static synchronized void init() {
        if (mInstance == null) {
            mInstance = new CarSignalController();
        }
    }

    private CarSignalController() {
        initICarManager();
    }

    private void initICarManager() {
        isInitOk = false;
        isInitIng = true;
        Context hostAppContext = PluginContext.getContextByActThread();
        LogUtils.e(TAG, "CarSignalController hostAppContext:" + hostAppContext);
        if (hostAppContext == null) {
            return;
        }
        Car.createCar(hostAppContext, ThreadUtils.getBackgroundHandler(),
                10000L, new Car.CarServiceLifecycleListener() {
                    @Override
                    public void onLifecycleChanged(Car car, boolean b) {
                        LogUtils.e(TAG, "Car.createCar b:" + b);
                        if (b) {
                            isInitOk = true;
                            carPropertyManager = (CarPropertyManager) car.getCarManager(Car.PROPERTY_SERVICE);
                            wrapper = com.ecarx.xui.adaptapi.car.Car.createWrapper(hostAppContext);
                        }
                        isInitIng = false;
                    }
                });
    }

    public static CarSignalController getInstance() {
        return mInstance;
    }

    public static boolean checkSelf() {
        if (mInstance == null) {
            return false;
        }
        CarPropertyManager carPropertyManager = mInstance.carPropertyManager;
        IWrapper wrapper = mInstance.wrapper;
        return carPropertyManager != null && wrapper != null;
    }

    public CarPropertyManager getCarPropertyManager() {
        return carPropertyManager;
    }

    public IWrapper getWrapper() {
        return wrapper;
    }

    public static FunctionStatus toFunctionStatus(int propertyStatus) {
        switch (propertyStatus) {
            case CarPropertyValue.STATUS_AVAILABLE:
                return FunctionStatus.active;
            case CarPropertyValue.STATUS_UNAVAILABLE:
                return FunctionStatus.notactive;
            case CarPropertyValue.STATUS_ERROR:
                return FunctionStatus.error;
            default:
                return FunctionStatus.notavailable;
        }
    }
}
